package jehc.djshi.sys.web;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jehc.djshi.common.annotation.Auth;
import jehc.djshi.common.base.BaseAction;
import jehc.djshi.common.base.BaseResult;
import jehc.djshi.common.base.BaseZTreeEntity;
import jehc.djshi.common.idgeneration.SnowflakeIdWorker;
import jehc.djshi.common.util.JsonUtil;
import jehc.djshi.common.util.StringUtil;
import jehc.djshi.sys.model.XtAreaRegion;
import jehc.djshi.sys.service.XtAreaRegionService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * @Desc 行政区划
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/xtAreaRegion")
@Api(value = "行政区划API",tags = "行政区划API",description = "行政区划API")
public class XtAreaRegionController extends BaseAction {

	@Resource
	private XtAreaRegionService xtAreaRegionService;

	@Resource
	SnowflakeIdWorker snowflakeIdWorker;

	/**
	* 查询行政区划
	* @param request 
	*/
	@ApiOperation(value="查询行政区划", notes="查询行政区划")
	@GetMapping(value="/list")
	@Auth(value = "/xtAreaRegion/list",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	public BaseResult getXtAreaRegionListByCondition(HttpServletRequest request){
		List<BaseZTreeEntity> list = new ArrayList<BaseZTreeEntity>();
		Map<String,Object> condition = new HashMap<String,Object>();
		List<XtAreaRegion> xtAreaRegionList = xtAreaRegionService.getXtAreaRegionListByCondition(condition);
		for(int i = 0; i < xtAreaRegionList.size(); i++){
			XtAreaRegion xtAreaRegion = xtAreaRegionList.get(i);
			BaseZTreeEntity BaseZTreeEntity = new BaseZTreeEntity();
			BaseZTreeEntity.setId(xtAreaRegion.getId());
			BaseZTreeEntity.setPId(""+xtAreaRegion.getParentId());
			BaseZTreeEntity.setText(xtAreaRegion.getName());
			BaseZTreeEntity.setName(xtAreaRegion.getName());
			BaseZTreeEntity.setTempObject(""+xtAreaRegion.getRegionLevel());
			BaseZTreeEntity.setContent(xtAreaRegion.getNameEn());
			BaseZTreeEntity.setIntegerappend("经度："+xtAreaRegion.getLongitude()+"<br>纬度："+xtAreaRegion.getLatitude());
			list.add(BaseZTreeEntity);
		}
		BaseZTreeEntity baseZTreeEntity = new BaseZTreeEntity();
		List<BaseZTreeEntity> baseZTreeEntityList = baseZTreeEntity.buildTree(list,"0");
		String json = JsonUtil.toFastJson(baseZTreeEntityList);
		return outStr(json);
	}

	/**
	* 查询单个行政区划
	* @param id
	*/
	@ApiOperation(value="查询单个行政区划", notes="根据Id查询单个行政区划")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/xtAreaRegion/get",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	public BaseResult<XtAreaRegion> getXtAreaRegionById(@PathVariable("id") String id){
		XtAreaRegion xtAreaRegion = xtAreaRegionService.getXtAreaRegionById(id);
		return outDataStr(xtAreaRegion);
	}

	/**
	 *查询全部行政区划
	 * @return
	 */
	@ApiOperation(value="查询全部行政区划", notes="查询全部行政区划")
	@GetMapping(value="/lists")
	@Auth(value = "/xtAreaRegion/lists",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	public BaseResult lists(){
		Map<String,Object> condition = new HashMap<String,Object>();
		List<XtAreaRegion> xtAreaRegionList = xtAreaRegionService.getXtAreaRegionListByCondition(condition);
		return outDataStr(xtAreaRegionList);
	}

	/**
	* 添加
	* @param xtAreaRegion
	*/
	@PostMapping(value="/add")
	@ApiOperation(value="创建行政区划", notes="创建行政区划")
	public BaseResult addXtAreaRegion(@RequestBody XtAreaRegion xtAreaRegion){
		int i = 0;
		if(null != xtAreaRegion){
			xtAreaRegion.setId(""+snowflakeIdWorker.nextId());
			xtAreaRegion.setCreateId(getXtUid());
			xtAreaRegion.setCreateTime(getDate());
			i=xtAreaRegionService.addXtAreaRegion(xtAreaRegion);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 修改
	* @param xtAreaRegion
	*/
	@PutMapping(value="/update")
	@ApiOperation(value="编辑行政区划", notes="编辑行政区划")
	public BaseResult updateXtAreaRegion(@RequestBody XtAreaRegion xtAreaRegion){
		int i = 0;
		if(!StringUtil.isEmpty(xtAreaRegion.getId())){
			xtAreaRegion.setUpdateId(getXtUid());
			xtAreaRegion.setUpdateTime(getDate());
			i=xtAreaRegionService.updateXtAreaRegion(xtAreaRegion);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 删除
	* @param id
	*/
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除行政区划", notes="删除行政区划")
	public BaseResult delXtAreaRegion(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i=xtAreaRegionService.delXtAreaRegion(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 复制一行并生成记录
	* @param ID
	*/
	@GetMapping(value="/copy")
	@ApiOperation(value="复制并生成行政区划", notes="复制并生成删除行政区划")
	public BaseResult copyXtAreaRegion(String ID){
		int i = 0;
		XtAreaRegion xtAreaRegion = xtAreaRegionService.getXtAreaRegionById(ID);
		if(null != xtAreaRegion){
			xtAreaRegion.setId(""+snowflakeIdWorker.nextId());
			xtAreaRegion.setCreateId(getXtUid());
			xtAreaRegion.setCreateTime(getDate());
			i=xtAreaRegionService.addXtAreaRegion(xtAreaRegion);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
}
